<?php
class Stok_model extends CI_Model{

	function   __construct() {
        parent::__construct();
    }
   //page
	private $tbl = 'stok';

	function selectAll()
	{
		return $this->db->get($this->tbl)->result();
	}

	function get_list_data($limit = 10, $offset = 0)
  	{
            if($offset==""){ $offset=0; }
	$query = $this->db->query("SELECT a. * , b.strnama, strnama_cabang
            FROM stok a, barang b, cabang c
            WHERE a.intid_barang = b.intid_barang and
					a.intid_cabang = c.intid_cabang
            ORDER BY intid_stok ASC LIMIT $offset,$limit");
		return $query->result();
         }

	function countData()
	{
	  	return $this->db->count_all($this->tbl);
  	}
//end page
      //test
    function selectDetail($id){
       $query = $this->db->query("SELECT a. * , b.strnama, strnama_cabang, d.strnama_jsatuan
            FROM stok a, barang b, cabang c, jenis_satuan d
            WHERE a.intid_barang = b.intid_barang and
		a.intid_cabang = c.intid_cabang and 
		a.intid_jsatuan = d.intid_jsatuan and
		a.intid_stok= '$id'");
	   return $query->result();
    }
 //end test
    function getStok(){

	   $query = $this->db->query("SELECT a. * , b.strnama
            FROM stok a, barang b
            WHERE a.intid_barang = b.intid_barang
            ORDER BY intid_stok ASC ");
           //$this->db->limit($limit, $offset);
	   return $query->result();
    }
        function count_all()
	{
		return $this->db->count_all($this->table);
	}

	function insert($data){
    $tgl = date("Y-m-d");
	
	// ini nih kode buat ngebenerinnya //
			
	$upil = $this->input->post('intid_barang');
	$this->db->select('intid_barang','strnama');
	$this->db->from('barang');
	$this->db->where('strnama',$upil);
	$query = $this->db->get();

	$row = $query->row();
	$we = $row->intid_barang;	
	
	// akhir kodenya intid_barang => we,
	
	$data = array(
            'intid_barang' => $we,
            'intqty' => $this->input->post('intqty'),
            'supplier' => $this->input->post('supplier'),
            'tanggal' => $tgl
            
            );
        $this->db->insert('stok', $data);
	}


    function delete($id){
       $this->db->where('intid_stok', $id);
       $this->db->delete('stok');
    }

	function select($id){
       $query = $this->db->query("select * from stok where intid_stok= $id");
	   return $query->result();
    }

    function update($id){
            $tgl = date("Y-m-d");
			
	// ini nih kode buat ngebenerinnya //
			
	$upil = $this->input->post('intid_barang');
	$this->db->select('intid_barang','strnama');
	$this->db->from('barang');
	$this->db->where('strnama',$upil);
	$query = $this->db->get();

	$row = $query->row();
	$we = $row->intid_barang;	
	
	// akhir kodenya intid_barang => we,
        $data = array(	//tambahan
          'intid_barang' => $we,
            'supplier' => $this->input->post('supplier'),
            'tanggal' => $tgl,
            'intqty' => $this->input->post('intqty')
            ); //akhir tambahan
        $this->db->where('intid_stok', $id);
		$this->db->update('stok', $data);   //tambahn , $data
	}

     function selectBarang($keyword){
        $query = $this->db->query("select intid_barang, upper(strnama) strnama from barang where strnama like '$keyword%'");
        return $query->result();
	}
    function search($key)
	{
		$this->db->like('strnama',$key);
		return $this->db->get($this->tbl);
	}
}
?>